package com.ruoyi.project.drug.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.drug.domain.DrugSupplier;
import com.ruoyi.project.drug.service.DrugSupplierService;
import com.ruoyi.project.system.domain.SysPost;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/drug/supplier")
public class DrugSupplierController extends BaseController {
    @Autowired
    private DrugSupplierService drugSupplierService;


    //查询
    @GetMapping("/list")
    @PreAuthorize("@ss.hasAnyPermi('drug:supplier:list')")
    public TableDataInfo list(DrugSupplier drugSupplier){
        startPage();
        List<DrugSupplier> list= drugSupplierService.selectDrugList(drugSupplier);
        return getDataTable(list);
    }


    //新增
    @PreAuthorize("@ss.hasPermi('drug:supplier:add')")
    @Log(title = "供应商添加", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add( @RequestBody DrugSupplier drugSupplier){
        drugSupplier.setCreateBy(SecurityUtils.getUsername());
        return toAjax(drugSupplierService.insertSupplier(drugSupplier));
    }

    //修改
    @PreAuthorize("@ss.hasPermi('drug:supplier:query')")
    @GetMapping(value = "/{supplierId}")
    public AjaxResult getInfo(@PathVariable Long supplierId)
    {
        return AjaxResult.success(drugSupplierService.selectSupplierById(supplierId));
    }

    @PreAuthorize("@ss.hasPermi('drug:supplier:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DrugSupplier drugSupplier)
    {

        drugSupplier.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(drugSupplierService.updateSupplier(drugSupplier));
    }

    //删除
    @PreAuthorize("@ss.hasPermi('drug:supplier:remove')")
    @Log(title = "供应商删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supplierIds}")
    public AjaxResult remove(@PathVariable Long[] supplierIds)
    {
        return toAjax(drugSupplierService.deleteSupplierByIds(supplierIds));
    }
}
